<?php
namespace app\admin\controller;
use think\Db;
class Sys extends Base
{    
    //编辑
    public function edit(){
        $rs = db('sys')->select();
        $info = [
            'site'  => [],
            'pic'   => [],
            'app'   => [],
            'sys'   => [],
        ];
        foreach($rs as $v){
            $info[$v['type']][] = $v;
        }
        $data = [
            'nav'       => ['系统管理', '系统设置'],
            'info'      => $info,
        ];
        return view('', $data);
    }
    
    /**
     * 修改值
     */
    public function editVal(){
        if(is_post()){
            $data['id']  = input('id');
            $data['v']   = input('val');
            Db::name('sys')->update($data);
            com_check(['suc'=>['msg'=>'编辑成功!', 'js_action'=>'alert']]);
        }
    }
    
    /**
     * 上传图片
     */
    public function editPic(){
        if( is_post() ){
            $data['id'] = input('id');
            if( $data['id']==150 ){
                if(!isset($_FILES['pic']) || $_FILES['pic']['type'] != 'image/x-icon'){
                    com_error(['请上传正确的图标格式']);
                }else{
                    //上传图标
                    $move_file = move_uploaded_file($_FILES['pic']['tmp_name'], "favicon.ico");
                    if(!$move_file) {
                        com_error(['上传图标失败']);
                    }else{
                        $pic = "./favicon.ico";
                    }   
                }
            }else{
                $pic        = com_up_pic('sys', 'pic', true);
            }
            com_check( ['err'=>['js_action'=>'alert'], 'suc'=>['return_type'=>'return']]);
            
            $data['v'] = $pic;
            Db::name('sys')->update($data);
            com_check(['suc'=>['msg'=>'上传成功!', 'js_action'=>'alert|f5']]);
        }
    }
    
    /**
     * 删除图片
     */
    public function delPic(){
        if( is_post() ){
            $field        = input('field');
            $data['id']   = input('id');
            $data[$field] = '';
            Db::name('sys')->update($data);
            com_check(['suc'=>['msg'=>'删除图片成功!', 'js_action'=>'alert|f5']]);
        }
    }
}